function isEmpty(item) {
  if (item != undefined && item != null && item != "") {
    return false;
  }
  return true;
}

function loadingDialog() {
  var  load = layer.load(1, {
    shade: [0.1,'#fff']
  });
  return load;
}

function dataCompare(dateA,dateB) {
  if(isEmpty(dateA)||isEmpty(dateB)){
    return 0;
  }

  var d1 = dateA.replace(/\-/g, "");
  var d2 = dateB.replace(/\-/g, "");

  return d2-d1;
}

function  initDatePicker() {
  $('.form_date').each(function (index, element) {
    $(element).datetimepicker({
      autoclose: 1,
      toggleActive:1,
      todayHighlight: 1,
      startView: 2,
      minView: "month",
      format: 'yyyy-mm-dd',
      singleDatePicker: true,
      calender_style: "picker_3"
    }, function(start, end, label) {
      console.log(start.toISOString(), end.toISOString(), label);
    });
  })
}

function dateFormatUtil(longTypeDate){
  if(isEmpty(longTypeDate)||longTypeDate=='0'){
    return '-';
  }
  return getDate(longTypeDate);
}

function getDate(tm){
  var date = new Date(parseInt(tm) * 1000);
  var y = date.getUTCFullYear();
  var m = date.getMonth() + 1;
  m = m < 10 ? ('0' + m) : m;
  var d = date.getDate();
  d = d < 10 ? ('0' + d) : d;
  var h = date.getHours();
  h = h < 10 ? ('0' + h) : h;
  var minute = date.getMinutes();
  var second = date.getSeconds();
  minute = minute < 10 ? ('0' + minute) : minute;
  second = second < 10 ? ('0' + second) : second;
  return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;
}

/**
 * ajax
 *
 * @param extendUrl
 * @param callback
 * @param data
 */
function  comAjax(extendUrl,callback,data) {
  var load =loadingDialog();
  if(isEmpty(data)){
    $.ajax({
      type: 'POST',
      timeout: 10000,
      beforeSend: function (xhr) {
        xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
      },
      url: extendUrl,
      success: function (result) {
        callback(result);
        closeAllLayer();
      },
      complete: function (xhr, status) {
        closeAllLayer();
      }, error: function (XMLHttpRequest, textStatus, errorThrown) {
        console.log('Error：' + errorThrown);
        closeAllLayer();
      }
    });
  }else{
    $.ajax({
      type: 'POST',
      timeout: 10000,
      beforeSend: function (xhr) {
        xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
      },
      url: extendUrl,
      dataType: 'json',
      data: data,
      success: function (result) {
        callback(result);
        closeAllLayer();
      },
      complete: function (xhr, status) {
        closeAllLayer();
      }, error: function (XMLHttpRequest, textStatus, errorThrown) {
        closeAllLayer();
        console.log('Error：' + errorThrown);
      }
    });
  }
}

function closeAllLayer() {
  closeLayerDialog();
  closeLayerLoading();
  closeLayerPage();
}

function closeLayerDialog() {
  $(".layui-layer-dialog").each(function (index, element) {
    var id = $(element).attr("times");
    if (!isEmpty(id)) {
      layer.close(id);
    }
  })
}

function closeLayerLoading() {
  $(".layui-layer-loading").each(function (index, element) {
    var id = $(element).attr("times");
    if (!isEmpty(id)) {
      layer.close(id);
    }
  })
}

function closeLayerPage() {
  $(".layui-layer-page").each(function (index, element) {
    var id = $(element).attr("times");
    if (!isEmpty(id)) {
      layer.close(id);
    }
  })
}

function validate(formId) {

  var valid = $('#' + formId).validate({
    debug : true,
    focusInvalid : true,
    onfocusout : function(element) {
      this.element(element);
    },
  }).form();
  return valid;
}

function resetValidate(formId) {
  $('#' + formId).validate({
    debug : true,
    focusInvalid : true,
    onfocusout : function(element) {
      this.element(element);
    },
  }).resetForm();
}

// /**
//  * FormToJson
//  */
// (function($){
//   $.fn.serializeJson=function(){
//     var serializeObj={};
//     var array=this.serializeArray();
//     var str=this.serialize();
//     $(array).each(function(){
//       if(serializeObj[this.name]){
//         if($.isArray(serializeObj[this.name])){
//           serializeObj[this.name].push(this.value);
//         }else{
//           serializeObj[this.name]=[serializeObj[this.name],this.value];
//         }
//       }else{
//         serializeObj[this.name]=this.value;
//       }
//     });
//     return serializeObj;
//   };
// })(jQuery);
